<?php

namespace app\admin\controller;
use app\common\controller\Base;

class Article extends Base
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        $page = input('get.page',1);
        $numPage = 1;
        $startPage = ($page-1) * $numPage;
        $count = db('article')->count();
        $data = db('article')->limit($startPage,$numPage)->select();
        $pages = ceil(($count/$numPage));
        $this->assign('page',$page);
        $this->assign('pages',$pages);
        $data = editTime($data);
        $this->assign('data',$data);
        $this->assign('count',$count);
        return view();
    }

    /**
     * 显示创建资源表单页.
     */
    public function create()
    {
        $data = db('acolumn')->field('id,title')->select();
        $this->assign('data',$data);
        return view();
    }

    /**
     * 显示编辑资源表单页.
     */
    public function edit()
    {
        $id = input('get.id/d','');
        $data = db('article')->where('id',$id)->find();
        $info = db('acolumn')->field('id,title')->select();
        $this->assign('id',$id);
        $this->assign('data',$data);
        $this->assign('info',$info);
        return view();
    }

    /**
     * 保存新建的资源
     */
    public function save()
    {
        $data['id'] = input('post.id','');
        $data['title'] = input('post.title','', '/([\x{4e00}-\x{9fa5}]+)/u');
        $data['title'] || jump_page('','资讯标题必须为汉字!',2);
        $data['cid'] = input('post.cid','');
        $data['cid'] || jump_page('','请选择资讯分类!',2);
        $data['source'] = input('post.source','');
        $data['source'] || jump_page('','资讯来源不为空!',2);
        $data['content'] = input('post.content','', '/([\x{4e00}-\x{9fa5}]+)/u');
        $data['content'] || jump_page('','资讯内容必须为汉字!',2);
        $data["image"] = update_image('img',400000,'date','/static/images/');
        if($data['id']){
            if(!$data["image"]){
                unset($data["image"]);
            }else{
                delete_image('article',$data['id']);
            }
            $article = db('article')->where('id', $data['id'])->update($data);
            return $article !== false ? _layer_msg('更新成功!','',1) : jump_page('','更新失败!',2);
        }
        $data["image"] || jump_page('','请上传图片!',2);
        $data['addTime'] = time();
        db('article')->insert($data) ? _layer_msg('已添加!','',1) : _layer_msg('添加失败!','admin/Article/index');
    }

    /**
     * 删除指定资源
     */
    public function delete()
    {
        $ids = rtrim(input('post.id',''),',');
        delete_image('article',$ids);
        if(!$ids) return json(['status'=>0,'msg'=>'请选择要删除的角色!']);
        return db('article')->delete($ids) ? json(['status'=>1,'msg'=>'已删除!']) : json(['status'=>0,'msg'=>'删除失败!']);
    }
    /**
     * 改变资讯状态
     */
    public function status(){
        $data['id'] = input('post.id/d','');
        $data['status'] = input('post.status/d','');
        if(!$data['id']) return json(['status'=>0,'msg'=>'请选择要更新的状态!']);
        if($data['status'] == 1){
            $data['status'] = 2;
            return db('article')->update($data) ? json(['status'=>1,'msg'=>'已审核']) : json(['status'=>0,'msg'=>'该操作暂不可执行!']);
        }
        if($data['status'] == 2){
            $data['status'] = 3;
            return db('article')->update($data) ? json(['status'=>2,'msg'=>'发布']) : json(['status'=>0,'msg'=>'该操作暂不可执行!']);
        }
        if($data['status'] == 3){
            $data['status'] = 4;
            return db('article')->update($data) ? json(['status'=>3,'msg'=>'已发布']) : json(['status'=>0,'msg'=>'该操作暂不可执行!']);
        }
        if($data['status'] == 4){
            $data['status'] = 3;
            return db('article')->update($data) ? json(['status'=>4,'msg'=>'下架']) : json(['status'=>0,'msg'=>'该操作暂不可执行!']);
        }

    }
}
